#!/bin/bash
ip="10.221.175.148"
#ip="10.220.5.71"
user="root"
password="123456"

logfile="etlload.log"
if [ ! -f "$logfile" ]; then
  touch "$logfile"
fi

if [ ! -n "$1" ]; then
  echo "code1 param is null!">> ${logfile}
  exit
elif [ ! -n "$2" ]; then
  echo "code2 param is null!">> ${logfile}
  exit
elif [ ! -n "$3" ]; then
  echo "code3 param is null!">> ${logfile}
  exit
elif [ ! -n "$4" ]; then
  echo "code4 param is null!">> ${logfile}
  exit
fi

echo "table name is $4 \n">> ${logfile}
echo "table name is $4 \n">> ${logfile}

sql="load data local infile '$2' into table $4 lines terminated by '\r\n';"
echo "$sql" >> ${logfile}

if [ $1 == "1" ]; then
  sql1="alter table $4 truncate partition part_$3;"
  mysql -h${ip} -P 8761 -u${user} -p${password} <<EOFMYSQL
    ${sql1};
    ${sql};
    exit
EOFMYSQL
elif [ $1 == "2" ]; then
  sql1="alter table $4 truncate partition part_$3_470;"
  sql2="alter table $4 truncate partition part_$3_471;"
  sql3="alter table $4 truncate partition part_$3_472;"
  sql4="alter table $4 truncate partition part_$3_473;"
  sql5="alter table $4 truncate partition part_$3_474;"
  sql6="alter table $4 truncate partition part_$3_475;"
  sql7="alter table $4 truncate partition part_$3_476;"
  sql8="alter table $4 truncate partition part_$3_477;"
  sql9="alter table $4 truncate partition part_$3_478;"
  sql10="alter table $4 truncate partition part_$3_479;"
  sql11="alter table $4 truncate partition part_$3_482;"
  sql12="alter table $4 truncate partition part_$3_483;"
  mysql -h${ip} -P 8761 -u${user} -p${password} <<EOFMYSQL
    ${sql1};
    ${sql2};
    ${sql3};
    ${sql4};
    ${sql5};
    ${sql6};
    ${sql7};
    ${sql8};
    ${sql9};
    ${sql10};
    ${sql11};
    ${sql12};
    ${sql};
    exit
EOFMYSQL

else
  sql1="truncate table $4;"
  mysql -h${ip} -P 8761 -u${user} -p${password} <<EOFMYSQL
    ${sql1};
    ${sql};
    exit
EOFMYSQL
fi
echo "import ends \n">> ${logfile}
